<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :lock-scroll="true"
    width="650px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      :status-icon="true"
      label-width="80px"
    >
      <el-form-item
        label="所属村"
        prop="villageWrap"
      >
        <el-cascader
          v-model="form.villageWrap"
          :options="villageCascader"
          :props="villageProp"
          placeholder="请选择所属村"
          clearable
          @change="change"
        />
      </el-form-item>
      <el-form-item
        label="村民"
        prop="villager"
      >
        <el-select
          v-model="form.villager"
          placeholder="请选择对应村民"
        >
          <el-option
            v-for="item in villagerAtt"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="类型"
        prop="type"
      >
        <el-select
          v-model="form.type"
          placeholder="类型"
        >
          <el-option
            v-for="(item, index) in typeAtt"
            :key="item"
            :label="item.label"
            :value="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="是否匿名"
        prop="anonymous"
      >
        <el-select
          v-model="form.anonymous"
          placeholder="请选择是否匿名 默认为 非匿名"
        >
          <el-option
            label="非匿名"
            :value="false"
          />
          <el-option
            label="匿名"
            :value="true"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="标题"
        prop="title"
      >
        <el-input
          v-model.trim="form.title"
          autocomplete="off"
          placeholder="请输入标题"
          clearable
        />
      </el-form-item>
      <el-form-item
        label="内容"
        prop="content"
      >
        <el-input
          v-model.trim="form.content"
          type="textarea"
          autocomplete="off"
          placeholder="请输入内容"
          clearable
        />
      </el-form-item>

      <relatedDocuments
        :related-documents="documents"
        @getRes="getResRelatedDocuments"
      />
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="close">
        取 消
      </el-button>
      <el-button
        type="primary"
        @click="save"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
//手动添加
import { mapGetters } from "vuex";
import relatedDocuments from "@/components/My-Components/relatedDocuments";
export default {
  name: "TableEdit",
  components: {
    relatedDocuments,
  },
  props: ["fetchData"],
  data() {
    return {
      form: {
        villageWrap: "",
        title: "",
        content: "",
        type: "",
        anonymous: false,
      },
      id: null,
      created: true,
      rules: {
        villageWrap: [{ required: true, trigger: "change", message: "请选择所属村" }],
        villager: [{ required: true, trigger: "change", message: "请选择对应村下的村民" }],
        title: [{ required: true, trigger: "blur", message: "请输入标题" }],
        type: [{ required: true, trigger: "change", message: "请选择反馈类型" }],
        anonymous: [{ required: false, trigger: "change", message: "请选择是否匿名" }],
        content: [{ required: true, trigger: "blur", message: "请输入内容" }],
      },
      dialogImageUrl: "",
      dialogFormVisible: false,
      dialogVisible: false,
      typeAtt: [
          {
            name: "funds",
            label: "资金"
          },
          //资产
          {
            name: "assets",
            label: "资产"
          },
          //资源
          {
            name: "resources",
            label: "资源"
          },
          //党务
          {
            name: "org",
            label: "党务"
          },
          //村务
          {
            name: "village",
            label: "村务"
          },
          //其他
          {
            name: "other",
            label: "其他"
          },
      ],
      villageCascader: [],
      villageProp: {
        value: "id",
        label: "name",
      },
      createForm: [],
      relatedDocuments: [],
      documents: "",
      villagerAtt: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    async showEdit(row) {
      if (!row) {
        this.title = "添加";
        this.created = true;
      } else {
        this.id = row.id;
        const res = await this.$http.get(`feedback/${this.id}`);

        this.documents = res.data.relatedDocuments
        this.relatedDocuments = res.data.relatedDocuments

        console.log(this.documents)
        
        const town = await this.$http.get(`village/${res.data.village.id}`);

        const area = await this.$http.get(`town/${town.data.town.id}`);

        res.data.villageWrap = [area.data.area.id, town.data.town.id, res.data.village.id];
        this.change(res.data.villageWrap)
        //处理关联
        res.data.village = res.data.village.id
        res.data.villager = res.data.villager.id

        this.title = "编辑";
        this.form = Object.assign({}, res.data);
        this.created = false;
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      this.documents = "[]"
      this.dialogFormVisible = false;
    },
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.resPip();
          if (this.created) {
            this.create();
          } else {
            this.edit();
          }
        } else {
          return false;
        }
      });
    },
    async create() {
      const res = await this.$http.post(
        `/feedback`,
        this.createForm
      );
      if (res.status == 201) {
        this.$baseMessage("新增成功", "success");
        this.$emit("fetchData");
        this.$refs["form"].resetFields();
        this.dialogFormVisible = false;
        this.form = this.$options.data().form;
      } else {
        this.$baseMessage("新增失败", "error");
      }
    },
    async edit() {
      const res = await this.$http.put(
        `/feedback/${this.id}`,
        this.createForm
      );
      if (res.status == 200) {
        this.$baseMessage("修改成功", "success");
        this.$emit("fetchData");
        this.$refs["form"].resetFields();
        this.dialogFormVisible = false;
        this.form = this.$options.data().form;
      } else {
        this.$baseMessage("修改失败", "error");
      }
    },
    resPip() {
      this.createForm = JSON.parse(JSON.stringify(this.form));
      this.createForm.relatedDocuments = this.relatedDocuments;
    },
    getResRelatedDocuments(val) {
      this.relatedDocuments = val
    },
    async change(val) {
      this.form.village = val[val.length - 1];
      const res = await this.$http.get(`village/${this.form.village}?join=villager`)
      this.villagerAtt = res.data.villager
    },
    async init() {
      const res = await this.$http.get(`area?join=town&join=town.village`);
      res.data.forEach(area => {
        area.children = area.town
        area.town.forEach(town => {
          town.children = town.village
        })
      })
      this.villageCascader = res.data;
    },
  },
};
</script>
